import HttpTool from '../base/Http/HttpTool';
import GameEntry from '../GameEntry';
import MyJs from '../plugin/MyJS';
import QuickTool from '../base/Tool/QuickTool';
export default class SaveData {
    static exitGame () {
        MyJs.callHandler("gameLeave", {state:'leave'}, data=>{

        })
    }

    static endGuide(complete: (level)=>void) {
        if ( !MyJs.callHandler("gameSwitch", {}, data=>{
            if ((window as any).isAndroid) {
                data = JSON.parse(data)
            }
            QuickTool.safeCall1(complete, parseInt(data.level))
        }) ) {
            QuickTool.safeCall1(complete, 1)
        }
    }

    static startCall (complete: ()=>void) {
        console.log("call gameStart")
        let success = MyJs.callHandler("gameStart", {state:"start"}, data=>{
            if ((window as any).isAndroid) {
                data = JSON.parse(data)
            }
            if (data.code && parseInt(data.code) == 200) {
                QuickTool.safeCall1(complete)
            }
        })
        if (!success) {
            QuickTool.safeCall1(complete)
        }
    }

    static overUpload (gameid: number, win: boolean, baseData: any, complete: (data)=>void) {
        let upload
        if (baseData instanceof SaveDataBase) {
            upload = this.save(gameid, win, baseData)
        }else{
            upload = baseData
            baseData.gameId = gameid
            baseData.lvWin = win?1:0
        }

        upload.state = "over"
        if (win) {
            upload.ask = "right"
        }else{ upload.ask = "error" }

        console.log("upload");
        console.log(upload);
        
        let success = MyJs.callHandler("gameOver", upload, data=>{
            if ((window as any).isAndroid) {
                data = JSON.parse(data)
            }
            
            if (data.code && parseInt(data.code) == 200) {
                GameEntry.ExtraObj = data
                GameEntry.Token = data.token
                data.level = parseInt(data.level)
                QuickTool.safeCall1(complete, data)
            }
        })

        if (!success) {
            QuickTool.safeCall1(complete, {level:1})
        }
    }

    static overCall (win:boolean, complete: ()=>void) {
        //ok  {state:'over',ask:'right'}
        //fail {state:'over',ask:'error'}
        let map
        if (win) {
            map = {state:'over',ask:'right'}
        }else{ map = {state:'over',ask:'error'} }

        console.log("call gameOver")
        let success = MyJs.callHandler("gameOver", map, data=>{
            // if (data == "overRecived") {
            //     QuickTool.safeCall1(complete)
            // }
            if ((window as any).isAndroid) {
                data = JSON.parse(data)
            }
            
            if (data.code && parseInt(data.code) == 200) {
                GameEntry.ExtraObj = data
                GameEntry.Token = data.token
                QuickTool.safeCall1(complete)
            }
        })

        if (!success) {
            QuickTool.safeCall1(complete)
        }
    }

    static save (gameid: number, win: boolean, baseData: SaveDataBase) : any{
        baseData.cat()
        let sData: any = {}
        if (GameEntry.ExtraObj) sData = GameEntry.ExtraObj

        if (gameid == 1) {
            let data = baseData as SaveData1
            data.calcRate()
            sData.bumpSum = data.hitCounts[0]
            sData.reactionRate = data.reactionRates[0].toString()
        }else if (gameid == 3) {
            let data = baseData as SaveData03

            sData.correctCount = data.correctCount
            sData.errorCount = data.errorCount
            sData.reactionRate = data.reactionRate.toFixed(2)
        }else if (gameid == 4) {
            let data = baseData as SaveData04

            sData.correcRate = data.correcRate.toFixed(2)
            sData.errorRate = data.errorRate.toFixed(2)
            sData.reactionRate = data.reactionRate.toFixed(2)
        }else if (gameid == 5) {
            let data = baseData as SaveData05

            sData.lvCorrectCount = data.lvCorrectCount
            sData.lvErrorCount = data.lvErrorCount
            sData.reactionRate = data.reactionRate.toFixed(2)
        }else if (gameid == 6) {
            let data = baseData as SaveData06

            sData.lvCorrectCount = data.lvCorrectCount
            sData.lvDismissCount = data.lvDismissCount
            sData.reactionRate = data.reactionRate.toFixed(2)
        }else if (gameid == 10) {
            let data = baseData as SaveData10

            sData.lvCorrectCount = data.lvCorrectCount
            sData.reactionRate = data.reactionRate.toFixed(2)
        }else if (gameid == 12) {
            let data = baseData as SaveData12

            // sData.lvCorrectTime = data.lvCorrectTime.toString()
            for (let i=0;i<data.chipTime.length;i++) {data.chipTime[i] = parseFloat(data.chipTime[i].toFixed(2))}
            sData.lvTime = data.chipTime.join(",")
            let sum = 0
            for (let i=0;i<data.chipTime.length;i++) {sum += data.chipTime[i]}
            sData.lvTimePer = (sum/data.chipTime.length).toFixed(2)
            // sData.lvCorrectCount = data.lvCorrectCount
            // sData.lvErrorCount = data.lvErrorCount
        }else if (gameid == 14) {
            let data = baseData as SaveData14

            sData.lvCompleteTimePer = data.lvCompleteTimePer.toFixed(2)
            sData.lvCorrectCount = data.lvCorrectCount
            sData.lvErrorCount = data.lvErrorCount
        }


        sData.lv = baseData.level
        sData.lvCostTime = baseData.useTime.toFixed(2)
        sData.lvWin = win?1:0
        sData.gameId = gameid
        // sData.token = GameEntry.Token

        // console.log(JSON.stringify(sData));
        // HttpTool.s_uploadData(sData)

        return sData
    }


    static r_nArray (): number[] {
        let s: number[] = [0,0,0,0,0,0,0,0,0,0]
        return s
    }
}

export class SaveDataBase {

    constructor (level: number, loop: number=1) {
        this.level = level
        this.loop = loop
    }

    //关卡
    level = 0 
    loop = 1

    //关卡用时
    useTime = 0
    //回合用时
    loopTimes = SaveData.r_nArray()

    addLoop (dt, loop=0) {
        if (this.loopTimes.length <= loop) {
            this.loopTimes.push(0)
        }

        this.loopTimes[loop] += dt
    }

    addTime (dt) {
        this.useTime += dt
    }

    cat () {
        this.loopTimes = this.loopTimes.map(item=>{
            return parseFloat(item.toFixed(3))
        })
        this.loopTimes.splice(this.loop, this.loopTimes.length-this.loop)
    }
}

export class SaveData1 extends SaveDataBase{

    //撞击次数
    hitCounts = SaveData.r_nArray()
    //回合反应率
    reactionRates = SaveData.r_nArray()

    calcRate () {
        
        for (let i=0;i<this.loopTimes.length;i++) {
            if (this.hitCounts[i] != 0) {
                this.reactionRates[i] = parseFloat((this.hitCounts[i] / this.loopTimes[i]).toFixed(2))
            }
        }
    }

    cat () {
        super.cat()
        this.hitCounts.splice(this.loop, this.hitCounts.length-this.loop)
        this.reactionRates.splice(this.loop, this.reactionRates.length-this.loop)
    }
}

export class SaveData03 extends SaveDataBase{
    correctCount = 0
    errorCount = 0
    reactionRate = 0.0
}

export class SaveData04 extends SaveDataBase{
    correcRate = 0.0
    errorRate = 0.0
    reactionRate = 0.0
}

export class SaveData05 extends SaveDataBase{
    //消除了多少个
    lvCorrectCount = 0
    //错误了多少次
    lvErrorCount = 0
    //
    reactionRate = 0.0
}

export class SaveData06 extends SaveDataBase{
    lvCorrectCount = 0
    lvDismissCount = 0
    reactionRate = 0.0
}

export class SaveData10 extends SaveDataBase{
    lvCorrectCount = 0
    reactionRate = 0.0
}

export class SaveData12 extends SaveDataBase{
    // lvCorrectTime = 0.0
    lvTime = 0.0
    lvTimePer = 0.0
    chipTime = []

    // lvCorrectCount = 0
    // lvErrorCount = 0
}

export class SaveData14 extends SaveDataBase{

    lvCompleteTimePer = 0.0
    lvCorrectCount = 0
    lvErrorCount = 0
}

/**
 * public void NOSomethingA()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingB()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingC()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingD()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingE()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingF()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingG()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingH()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingI()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingJ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingK()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingL()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingM()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingN()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingO()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingP()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingQ()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingR()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingS()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingT()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingU()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
public void NOSomethingV()
{
    string name = "NoName";
    string first = "what monther's is who?011245";
}
 */
